﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Model;
using System.Collections.Generic;
using System.Linq;

namespace DAL
{
    /// <summary>
    /// 数据访问类:PMS_User
    /// </summary>
    public class PMS_UserDAL
    {
        public PMS_UserDAL()
        { }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="user">登录名</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public PMS_User Login(string user, string pwd)
        {
            string sql = "select * from dbo.PMS_User where isDelete='0' and loginName=@loginName and loginPassword=@pwd";
            SqlParameter[] values = {
                                    new SqlParameter("@loginName",user),
                                    new SqlParameter("@pwd",pwd)
                                    };
            return SqlHelper.GetDataByColumns<PMS_User>(Comm.Config.PMS, sql, values);
        }

        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.PMS_User model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@loginName", SqlDbType.NVarChar,50),
					new SqlParameter("@loginPassword", SqlDbType.NVarChar,50),
					new SqlParameter("@roleID", SqlDbType.Int,4),
                    new SqlParameter("@role",SqlDbType.NVarChar,50)};
            parameters[0].Value = model.loginName;
            parameters[1].Value = model.loginPassword;
            parameters[2].Value = model.roleID;
            parameters[3].Value = model.role;

            int count = SqlHelper.ExecuteNonQuery(Comm.Config.PMS, CommandType.StoredProcedure, "sp_AddUserList",parameters);
            if (count > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.PMS_User model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PMS_User set ");
            strSql.Append("loginName=@loginName,");
            strSql.Append("loginPassword=@loginPassword,role=@role");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@loginName", SqlDbType.NVarChar,50),
					new SqlParameter("@loginPassword", SqlDbType.NVarChar,50),
					new SqlParameter("@role", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.loginName;
            parameters[2].Value = model.loginPassword;
            parameters[3].Value = model.role;

            int rows = SqlHelper.ExecuteNonQuery(Comm.Config.PMS, CommandType.Text, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PMS_User set isDelete='1' ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = { new SqlParameter("@id", id) };
            int rows = SqlHelper.ExecuteNonQuery(Comm.Config.PMS, CommandType.Text, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<PMS_User> GetList(string strWhere)
        {
            string strSql = "select * FROM PMS_User where isDelete='0' ";
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql += " and " + strWhere;
            }
            return SqlHelper.GetDataByColumnsLst<PMS_User>(Comm.Config.PMS, strSql);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<PMS_User> GetList(int roleID)
        {
            string strSql = "select * FROM PMS_User where isDelete='0' and roleID=@roleID";
            SqlParameter[] values = { new SqlParameter("@roleID", roleID) };
            return SqlHelper.GetDataByColumnsLst<PMS_User>(Comm.Config.PMS, strSql, values);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public PMS_User GetModel(int id)
        {
            string strSql = "select * FROM PMS_User where isDelete='0' and id=@id";
            SqlParameter[] values = { new SqlParameter("@id", id) };
            return SqlHelper.GetDataByColumns<PMS_User>(Comm.Config.PMS, strSql, values);
        }

        #endregion  Method
    }
}

